import os
import sys

from common import StockDict
from common.utils import DateUtil

base_path = os.getcwd()
father_path = os.path.abspath(os.path.dirname(base_path) + os.path.sep + ".")
sys.path.append(base_path)


def gen_path(local_path):
    if not os.path.exists(local_path):
        os.makedirs(local_path)  # 创建一个文件夹


def del_null_doc(local_path):
    today_str = DateUtil.get_date_str()
    # 删除空结果
    validation_dict = []
    for root, dirs, files in os.walk(local_path):
        for file in files:
            src_file = os.path.join(root, file)
            if os.path.getsize(src_file) == 0:
                print("删除" + file)
                os.remove(src_file)
            else:
                validation_dict.append(file.split('.')[0])
    # 生成验证文件
    validation_result = open(father_path + '\\result\\双阳夹阴\\验证' + today_str + '.txt', mode='w', encoding='utf-8')
    for s_code in validation_dict:
        if s_code not in StockDict:
            continue
        validation_result.write(str(StockDict[s_code]) + '\n')
        validation_result.write('http://stockpage.10jqka.com.cn/' + s_code)
        validation_result.write('/#refCountId=stockpage_5c3e9aef_93' + '\n')
    validation_result.close()


def del_blank_doc(local_path):
    for root, dirs, files in os.walk(local_path):
        for file in files:
            src_file = os.path.join(root, file)
            if os.path.getsize(src_file) == 0:
                print("删除" + file)
                os.remove(src_file)


def get_file_names(local_path):
    validation_dict = []
    for root, dirs, files in os.walk(local_path):
        for file in files:
            src_file = os.path.join(root, file)
            if os.path.getsize(src_file) == 0:
                print("删除" + file)
                os.remove(src_file)
            else:
                validation_dict.append(file.split('.')[0])
    return validation_dict


def get_file_path_names(local_path):
    validation_dict = []
    for root, dirs, files in os.walk(local_path):
        for file in files:
            src_file = os.path.join(root, file)
            if os.path.getsize(src_file) == 0:
                print("删除" + file)
                os.remove(src_file)
            else:
                validation_dict.append(root + '\\' + file)
    return validation_dict


# result = []
def get_doc_info(doc):
    result = []
    while 1:
        line = doc.readline()
        if line == '':
            return result
        line = line[:-1]
        if line == '':
            return result
        result.append(line)


# result = []
def get_path_doc_info(local_path):
    doc = open(local_path, mode='r', encoding='utf-8')
    result = []
    while 1:
        line = doc.readline()
        if line == '':
            return result
        line = line[:-1]
        if line == '':
            return result
        result.append(line)


# data = [] 每个list中参数是一行
# name 文件名称
def gen_new_doc(local_path, name, data):
    new_doc = open(local_path + '\\' + name, mode='w', encoding='utf-8')
    for line in data:
        new_doc.write(str(line) + '\n')
    new_doc.close()


if __name__ == '__main__':
    print()
    path = father_path + '\\common\\funds\\detail\\'
    print(path)
    del_null_doc(path)
